* This File Runs and Plots Coefficients for Event Study Regressions of Instutional Ownership
eststo clear
clear all
use ../dta/io_stacked.dta, clear

* Generating Event Study Variables
gen any_vintage=vintage!=.
egen date_cohort=group(date2 cohort)
egen code_cohort=group(code cohort)
gen quarters_vintage=quarters_until_target*any_vintage 
gen quarters_until_target_aux=quarters_until_target


* Labeling For Plots
local mf_ownership_total_l =  "Mutual Fund Ownership Share"
local top10_ownership_total_l = "Top 10 Ownership Share"


// Plots for 12-10 Window
foreach var in top10_ownership_total mf_ownership_total {
	foreach window in  12{
			foreach end in 6{
				preserve
				*locals
				local window1=`window'+1
				local end1 = `end'+1			
				* Running Event Study Regression For Ownership Share
				local window10=`window'+`end'
				keep if quarters_until_target>=-`window' & quarters_until_target<=`end'
				replace quarters_vintage=quarters_vintage+`window' if any_vintage==1
				reghdfe `var' i.quarters_vintage   if `var'<=1, a(date_cohort code_cohort) cl(code)
				
				* Save coefficients and variance-covariance matrix
				matrix b = e(b)
				matrix V = e(V)
				
				* Create variables to hold coefficients and confidence intervals
				gen double coef = .
				gen double lb = .
				gen double ub = .
				gen quarters_vintage_var = .
				
				* Loop through the quarters_vintage dummies to extract coefficients and confidence intervals
				forvalues i = 1/`window10' {
					local j = `i' +1
					local var_name "`i'.quarters_vintage"
					cap local coef_val = b[1, "`var_name'"]
					cap local se_val = sqrt(V["`var_name'", "`var_name'"])
					local lb_val = `coef_val' - 1.96 * `se_val'
					local ub_val = `coef_val' + 1.96 * `se_val'
					
					cap replace coef = `coef_val' in `j'
					cap replace lb = `lb_val' in `j'
					cap replace ub = `ub_val' in `j'
					cap replace quarters_vintage_var = `i'-`window' in `j'
				}
				replace quarters_vintage_var=-`window' in 1
				replace coef=0 in 1
				if "`var'"=="top10_ownership_total"{				
				
				* Create the  plot with 95% confidence intervals
				   twoway (rcap lb ub quarters_vintage_var, color(red%60)) ///
						(scatter coef quarters_vintage_var, color(red%60) msymbol(circle_hollow) msize(medium)) ///
						(line coef quarters_vintage_var, color(red%60)), ///
						ytitle("``var'_l'") xtitle("Quarters to Marginability")  ylabel(-0.04(0.02)0.08) yscale(range(-0.04 0.08))  ///
						legend(off)  xline(0, lcolor(black))  yline(0, lcolor(black)) ///
						xscale(range(-`window1' `end1')) 
				   
					graph export ../gph/figure5a.pdf, replace
				}
				else if "`var'"=="mf_ownership_total"{
					   twoway (rcap lb ub quarters_vintage_var, color(red%60)) ///
						(scatter coef quarters_vintage_var, color(red%60) msymbol(circle_hollow) msize(medium)) ///
						(line coef quarters_vintage_var, color(red%60)), ///
						ytitle("``var'_l'") xtitle("Quarters to Marginability")  ylabel(-0.01(0.005)0.02)  ///
						legend(off)  xline(0, lcolor(black))  yline(0, lcolor(black)) ///
						xscale(range(-`window1' `end1')) 
			   
				graph export ../gph/figure5b.pdf, replace			
			}
			restore
		}
	}
}
